/**
 * ZHU framework. Copyright 2012-, all rights reserved.
 *
 * $Id: PeriodController.java 47 2012-09-20 06:31:35Z zhuxiaopeng $
 * $Revision: 47 $
 * $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 * $Author: zhuxiaopeng $
 */
package daily.web.excel;

import static zhu.framework.web.WebUtility.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;

import javax.ejb.*;
import javax.enterprise.context.*;
import javax.inject.*;

import org.apache.poi.hssf.usermodel.*;

import zhu.framework.web.*;
import zhu.framework.web.jsf.*;
import daily.service.*;
import daily.util.*;
import daily.web.SessionBean;

/**
 * <p>
 * </p>
 * 
 * @author $Author: zhuxiaopeng $
 * @version $Revision: 47 $ - $Date: 2012-09-20 14:31:35 +0800 (四, 2012-09-20) $
 */
@Named
@RequestScoped
public class ExcelController extends ConversationalController {

	public String outputDay() {
		final Date date = new Date();
		final WebRequestContext webRequestContext = getWebRequestContext();
		final String outfilename = String.format("進捗報告（%s%d週）.xls", DateUtils.dateToTmperialString(date, "GGGGyyyy年M月"),
				DateUtils.getDayOfWeekInMonth(date));
		final WebDownloadContext downloadContext = new WebDownloadContext(WebDownloadContext.MS_EXCEL_CONTENT_TYPE, outfilename);
		try {
			try (final InputStream templateFile = getTemplateFile("daily.xls")) {
				final HSSFWorkbook createWorkBook = excelOutputService.createWorkBook(sessionBean.getStaffList(), templateFile);
				try (final OutputStream openDownloadStream = webRequestContext.openDownloadStream(downloadContext)) {
					createWorkBook.write(openDownloadStream);
				}
			}
		} catch (final IOException e) {
			e.printStackTrace();
		}
		webRequestContext.responseComplete();
		return null;
	}

	public String outputMonth() {
		final WebRequestContext webRequestContext = getWebRequestContext();
		final Date date = DateUtils.getDate(excelConversation.getYear(), excelConversation.getMonth(), 1);
		final String outfilename = String.format("日報（%s）_Educom.xls", DateUtils.dateToString(date, "yyyy年MM月"));
		final WebDownloadContext downloadContext = new WebDownloadContext(WebDownloadContext.MS_EXCEL_CONTENT_TYPE, outfilename);
		try {
			try (final InputStream templateFile = getTemplateFile("month.xls")) {
				final HSSFWorkbook createWorkBook = excelOutputService.createMonthWorkBook(sessionBean.getStaffList(), templateFile, date);
				try (final OutputStream openDownloadStream = webRequestContext.openDownloadStream(downloadContext)) {
					createWorkBook.write(openDownloadStream);
				}
			}
		} catch (final IOException e) {
			e.printStackTrace();
		}
		webRequestContext.responseComplete();
		return null;
	}

	private InputStream getTemplateFile(final String fileName) {
		final URL configFileURL = getClass().getClassLoader().getResource("/META-INF/" + fileName);
		InputStream templateFile = null;
		if (configFileURL != null) {
			try {
				templateFile = configFileURL.openStream();
			} catch (final IOException e) {
				e.printStackTrace();
				logger.log(Level.WARNING, "ファイル が開けない。", e);
				return null;
			}
		}
		return templateFile;
	}

	@EJB
	private ExcelOutputServiceImpl excelOutputService;

	@Inject
	private SessionBean sessionBean;

	@Inject
	private Logger logger;

	@Inject
	private ExcelConversation excelConversation;
}
